<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('指标名称')" prop="indicatorName">
            <el-input v-model="form.indicatorName" :placeholder="$t('指标名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('指标方向')" prop="indicatorDirection">
            <el-select v-model="form.indicatorDirection" :placeholder="$t('指标方向')">
              <el-option
                v-for="dict in indicatorDirectionOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <save-btn has-permi="deal:operational" />
    </div>
  </div>
</template>

<script>
    import { saveDealOperationalIndicatorsSetting, getDealOperationalIndicatorsSetting } from '@/api/deal/operational/setting/dealOperationalIndicatorsSetting'
    export default {
        data() {
            return {
                // 指标方向字典
                indicatorDirectionOptions: [], // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  indicatorName: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], indicatorDirection: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ] }
            }
        },
        created() {
            this.getDicts('indicator_direction').then(response => {
                this.indicatorDirectionOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                  getDealOperationalIndicatorsSetting(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                indicatorName: undefined,
                indicatorDirection: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                      this.loading = true
                      saveDealOperationalIndicatorsSetting(this.form).then(response => {
                        if (response.code === 200) {
                            this.msgSuccess(this.$t('保存成功'))
                            this.closeForm()
                        } else {
                            this.msgError(response.msg)
                        }
                        this.loading = false
                      })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
